package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.customerTypeCommand.AddCustomerTypeCommand;
import com.woniu.command.customerTypeCommand.DeleteCustomerTypeCommand;
import com.woniu.command.customerTypeCommand.UpdateCustomerTypeCommand;
import com.woniu.inlet.vo.CustomerTypeVo;
import com.woniu.outlet.po.CustomerTypePo;
import com.woniu.service.CustomerTypeService;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "客户类型管理")
@Slf4j
@RequestMapping("/customerType/")
public class CustomerTypeController {

    @Autowired
    private CustomerTypeService customerTypeService;

    @Autowired
    private SystemLogController systemLogController;

    @GetMapping("/customerTypes")
    @ApiOperation("查询客户分类")
    public ResponseResult<List<CustomerTypeVo>> findCustomerTypes() throws Exception{
        List<CustomerTypePo>  customerTypePos =  customerTypeService.findCustomerTypes();
        List<CustomerTypeVo> customerTypeVos = new ArrayList<>();
        //对象的深拷贝
        for (CustomerTypePo item : customerTypePos){
            CustomerTypeVo customerTypeVo = new CustomerTypeVo();
            BeanUtils.copyProperties(item,customerTypeVo);
            customerTypeVos.add(customerTypeVo);
        }
        systemLogController.addSystemLog("查询客户分类");
        return new ResponseResult<>(customerTypeVos, "OK", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/customerTypes/id")
    @ApiOperation("根据id查询客户分类")
    public ResponseResult<CustomerTypeVo> getCustomerTypeById(Long id) throws Exception {
        CustomerTypePo customerTypePo = customerTypeService.getCustomerTypeById(id);

        //对象的深拷贝

        CustomerTypeVo customerTypeVo = new CustomerTypeVo();
        BeanUtils.copyProperties(customerTypePo, customerTypeVo);

        systemLogController.addSystemLog("根据id查询客户分类");
        return new ResponseResult<>(customerTypeVo, "OK", ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/add")
    @ApiOperation("添加客户分类")
    @ApiOperationSupport(ignoreParameters = {"id","spare1","spare2","spare3"})
    public ResponseResult<String> addCustomerType(CustomerTypePo customerTypePo){
        Command command = new AddCustomerTypeCommand();
        command.execute(customerTypePo);

        systemLogController.addSystemLog("添加客户分类");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"添加成功",ResultCode.SUCCESS.getCode());
    }

    @PostMapping("/update")
    @ApiOperation("修改客户分类")
    @ApiOperationSupport(ignoreParameters = {"spare1","spare2","spare3"})
    public ResponseResult<String> updateCustomerType(CustomerTypePo customerTypePo){
        Command command = new UpdateCustomerTypeCommand();
        command.execute(customerTypePo);
        systemLogController.addSystemLog("修改客户分类");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"修改成功",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/delete")
    @ApiOperation("根据id删除客户")
    public ResponseResult<String> deleteCustomerType(Long id){
        Command command = new DeleteCustomerTypeCommand();
        command.execute(id);
        systemLogController.addSystemLog("根据id删除客户");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"删除成功",ResultCode.SUCCESS.getCode());
    }


}
